﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer;
namespace StoreWeb.system.purchaseManage
{
    public partial class list : System.Web.UI.Page
    {
        BLL.PurchaseMain purchaseMain = new BLL.PurchaseMain();
        //BLL.Role role = new BLL.Role();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //bindData(hidwhere.Value);
                int totalOrders = purchaseMain.GetRecordCount("");
                AspNetPager1.RecordCount = totalOrders;


                //打开页面时 将发货时间大于15天的自动确认收货
                List<Model.PurchaseMain> list = purchaseMain.GetModelList("orderstatus='已发货' and DATEDIFF(day, deliverytime, GETDATE()) > 15 ");//发货时间大于15天的
                foreach (var item in list)
                {
                    Model.PurchaseMain ne = purchaseMain.GetModel(item.id);
                    ne.orderstatus = "交易完成";
                    purchaseMain.Update(ne);
                }


            }
        }
        void bindData(string where)
        {

            Repeater1.DataSource = purchaseMain.GetListByPage(where, " ", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataBind();
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            bindData(hidwhere.Value);
        }

        /// <summary>
        /// 获得门店名
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        protected string GetDealersName(String dealersid)
        {
            if (dealersid == "" || dealersid == "0")
            {
                return "";
            }
            else
            {
                return new BLL.DealersManage().GetModel(Convert.ToInt32(dealersid)).username + "【" + new BLL.DealersManage().GetModel(Convert.ToInt32(dealersid)).dealersgrade + "级】";
                //return "";
            }
        }

        /// <summary>
        /// 截取长度
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        protected string GetLenth(String productname)
        {
            if (productname.Length>13)
            {
                return productname.Substring(0,13) +"...";
            }
            else
            {
                return productname;
            }
        }

        /// <summary>
        /// 获得用户名
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        protected string GetUserName(String createuser)
        {
            if (createuser == "")
            {
                return "";
            }
            else
            {
                return new Maticsoft.BLL.UserAdmin().GetModel(Convert.ToInt32(createuser)).username;
            }
        }


        protected void btnserach_Click(object sender, EventArgs e)
        {
            
            string where = "1=1 ";
            //if (type.Value != "")
            //{
            //    where += " and type = '" + type.Value + "'";
            //}
            if (!string.IsNullOrEmpty(startTime.Value) && !string.IsNullOrEmpty(endTime.Value))
            {
                where += " and createtime between '" + startTime.Value + "' and '" + endTime.Value + "'";
            }
            if (dealers.Value != null && !string.IsNullOrEmpty(dealers.Value))
            {
                where += " and dealersid = '" + dealers.Value + "'";
            }
            hidwhere.Value = where;
            bindData(hidwhere.Value);
        }


        /// <summary>
        /// 根据状态显示不同按钮
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        protected string GetOrderstatus(String orderstatus,String id)
        {
            if (orderstatus == "已付款")
            {
                return "<a title=\"审核\" href=\"javascript:;\" onclick=\"admin_role_confirm(this,'"+id+"')\" class=\"ml-5\" style=\"text-decoration: none\">审核</a>"+
                    "<a title=\"作废\" href=\"javascript:;\" onclick=\"admin_role_del(this,'" + id + "')\" class=\"ml-5\" style=\"text-decoration: none\">作废</a>";
            }
            else if (orderstatus == "已审核")
            {
                return "<a title=\"发货\" href=\"javascript:;\" onclick=\"admin_role_fh('发货','fh.aspx','" + id + "',600,210)\" class=\"ml-5\" style=\"text-decoration: none\">发货</a>";
            }
            else if (orderstatus == "已发货")
            {
                return "<a title=\"确认收货\" href=\"javascript:;\" onclick=\"admin_role_sh(this,'" + id + "')\" class=\"ml-5\" style=\"text-decoration: none\">确认收货</a>";
            }
            else {
                return "";
            }
        }

    }
}